package com.hskn.hss.module.tldictattentionitem.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tldictattentionitem.entity.TlEmergentDictAttentionItem;
import com.hskn.hss.module.tldictattentionitem.mapper.TlEmergentDictAttentionItemMapper;
import com.hskn.hss.module.tldictattentionitem.service.ITlEmergentDictAttentionItemService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-18
 */
@Service
public class TlEmergentDictAttentionItemServiceImpl extends ServiceImpl<TlEmergentDictAttentionItemMapper, TlEmergentDictAttentionItem> implements ITlEmergentDictAttentionItemService {

    @Resource
    TlEmergentDictAttentionItemMapper tlEmergentDictAttentionItemMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlEmergentDictAttentionItem tlEmergentDictAttentionItem = params.getObject("data", TlEmergentDictAttentionItem.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlEmergentDictAttentionItem.setCreateby(uid);
        tlEmergentDictAttentionItem.setCreatetime(new Date());
        tlEmergentDictAttentionItem.setDeptid(deptid);
        if (tlEmergentDictAttentionItem.getStatus() == null || tlEmergentDictAttentionItem.getStatus().isEmpty()) {
            tlEmergentDictAttentionItem.setStatus("1");
        }
        tlEmergentDictAttentionItemMapper.insert(tlEmergentDictAttentionItem);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlEmergentDictAttentionItem tlEmergentDictAttentionItem = params.getObject("data", TlEmergentDictAttentionItem.class);
        tlEmergentDictAttentionItem.setUpdateby(uid);
        tlEmergentDictAttentionItem.setUpdatetime(new Date());
        tlEmergentDictAttentionItemMapper.updateById(tlEmergentDictAttentionItem);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlEmergentDictAttentionItemMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlEmergentDictAttentionItem getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlEmergentDictAttentionItem tlEmergentDictAttentionItem = tlEmergentDictAttentionItemMapper.selectById(id);
        return tlEmergentDictAttentionItem;
    }

    @Override
    public List getSelectList(String classid) {
        List resultList = tlEmergentDictAttentionItemMapper.getSelectList(classid);
        return resultList;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        QueryWrapper<TlEmergentDictAttentionItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlEmergentDictAttentionItem::getClassid, params.getString("classid"));

        Page<TlEmergentDictAttentionItem> listPage = new Page<>(page, size, true);
        IPage<TlEmergentDictAttentionItem> iPage = tlEmergentDictAttentionItemMapper.selectPage(listPage, queryWrapper);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("resultlist", iPage.getRecords());
        return json;
    }
}
